package com.allwees.bs.c.module.app.service.impl;

import com.allwees.bs.c.module.app.entity.AppVersionUpdateEntity;
import com.allwees.bs.c.module.app.repository.AppVersionUpdateRepository;
import com.allwees.bs.c.module.app.service.IAppService;
import com.allwees.bs.c.module.app.vo.AppVersionUpdateVO;
import com.allwees.bs.c.module.app.vo.UserInfoCollectVo;
import com.allwees.bs.c.module.user.entity.UserInfoCollectEntity;
import com.allwees.bs.c.module.user.repository.UserInfoCollectRepository;
import com.allwees.core.common.constant.EPlatform;
import com.allwees.core.common.util.http.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


@Service
@Slf4j
public class AppServiceImpl implements IAppService {

    @Autowired
    private UserInfoCollectRepository userInfoCollectRepository;

    @Autowired
    private AppVersionUpdateRepository appVersionUpdateRepository;

    @Override
    public void setUserInfoCollect(String current_userUuid, UserInfoCollectVo userInfoCollectVo) {

        UserInfoCollectEntity userInfoCollectEntity = new UserInfoCollectEntity();

        userInfoCollectEntity.setDeviceUuid(userInfoCollectVo.getDeviceUuid());
        Example<UserInfoCollectEntity> example = Example.of(userInfoCollectEntity);

        boolean exists = userInfoCollectRepository.exists(example);
        if (exists) {
            return;
        }

        userInfoCollectEntity.setUserUuid(current_userUuid);
        userInfoCollectEntity.setGender(userInfoCollectVo.getGender());
        userInfoCollectRepository.save(userInfoCollectEntity);
    }

    public AppVersionUpdateVO checkAppVersion(String channel) {
        EPlatform ePlatform = RequestContext.getPlatform();
        Assert.isTrue(EPlatform.UNKNOWN != ePlatform, "platform unknown");
        String appVersion = RequestContext.getAppVersion();

        Assert.isTrue(appVersion != null, "app version is null");
        Assert.isTrue(channel != null, "channel is null");

        AppVersionUpdateVO appVersionUpdateVO = new AppVersionUpdateVO();
        AppVersionUpdateEntity appVersionUpdateEntity = appVersionUpdateRepository.findByChannelAndPlatform(ePlatform.name(), channel);
        validateAppVersion(appVersion, appVersionUpdateEntity, appVersionUpdateVO);
        return appVersionUpdateVO;
    }

    private void validateAppVersion(String currentVersion, AppVersionUpdateEntity appVersionUpdateEntity, AppVersionUpdateVO appVersionUpdateVO) {
        if (needUpdate(currentVersion, appVersionUpdateEntity)) {
            appVersionUpdateVO.setNeedUpdate(true);
            appVersionUpdateVO.setForceUpdate(appVersionUpdateEntity.getUpdateType() == 1);
            appVersionUpdateVO.setContent(appVersionUpdateEntity.getContent());
            appVersionUpdateVO.setDownUrl(appVersionUpdateEntity.getDownUrl());
            appVersionUpdateVO.setLatestVersion(appVersionUpdateEntity.getLatestVersion());
        }
    }

    private boolean needUpdate(String currentVersion, AppVersionUpdateEntity appVersionUpdateEntity) {
        if (appVersionUpdateEntity == null) {
            return false;
        }

        Assert.isTrue(currentVersion != null && currentVersion.contains("."), "version format wrong");
        String[] currentVersionArr = currentVersion.split("\\.");
        Assert.isTrue(currentVersionArr.length >= 3, "version format wrong");

        String minVersion = appVersionUpdateEntity.getMinVersion();
        Assert.isTrue(minVersion != null && minVersion.contains("."), "version format wrong");
        String[] minVersionArr = minVersion.split("\\.");
        Assert.isTrue(minVersionArr.length >= 3, "version format wrong");

        if (Integer.parseInt(currentVersionArr[0]) < Integer.parseInt(minVersionArr[0])) {
            return false;
        }

        if (Integer.valueOf(currentVersionArr[0]).equals(Integer.valueOf(minVersionArr[0])) && Integer.parseInt(currentVersionArr[1]) < Integer.parseInt(minVersionArr[1])) {
            return false;
        }

        if (Integer.valueOf(currentVersionArr[0]).equals(Integer.valueOf(minVersionArr[0])) && Integer.valueOf(currentVersionArr[1]).equals(Integer.valueOf(minVersionArr[1])) &&
                Integer.parseInt(currentVersionArr[2]) < Integer.parseInt(minVersionArr[2])) {
            return false;
        }

        String latestVersion = appVersionUpdateEntity.getLatestVersion();
        Assert.isTrue(latestVersion != null && latestVersion.contains("."), "version format wrong");
        String[] latestVersionArr = latestVersion.split("\\.");
        Assert.isTrue(latestVersionArr.length >= 3, "version format wrong");

        if (Integer.parseInt(currentVersionArr[0]) < Integer.parseInt(latestVersionArr[0])) {
            return true;
        }

        if (Integer.valueOf(currentVersionArr[0]).equals(Integer.valueOf(latestVersionArr[0])) && Integer.parseInt(currentVersionArr[1]) < Integer.parseInt(latestVersionArr[1])) {
            return true;
        }

        return Integer.valueOf(currentVersionArr[0]).equals(Integer.valueOf(latestVersionArr[0])) && Integer.valueOf(currentVersionArr[1]).equals(Integer.valueOf(latestVersionArr[1])) &&
                Integer.parseInt(currentVersionArr[2]) < Integer.parseInt(latestVersionArr[2]);
    }
}
